package jm.demo.jaxb.permission;

import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import com.sun.jersey.api.json.JSONJAXBContext;
import com.sun.jersey.api.json.JSONUnmarshaller;

@XmlRootElement
@XmlType(propOrder= {"contextType", "displayName", "roles"})
public class SecurityContextType {
	
	public static final String TYPE_SOLUTION_CDNMGR = "SCT_SOLUTION_CDNMGR";
	public static final String TYPE_SOLUTION_DSM = "SCT_SOLUTION_DSM";
	public static final String TYPE_SOLUTION_CMS = "SCT_SOLUTION_CMS";
	public static final String TYPE_ADM = "SCT_SOLUTION_CDNMGR";
	
	private String contextType;
    private String displayName;
    private List<Role> roles;
    
    
    
    
    public String getDisplayName() {
		return displayName;
	}
	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}
	
	
	
	
	public String getContextType() {
		return contextType;
	}
	public void setContextType(String contextType) {
		this.contextType = contextType;
	}
	public List<Role> getRoles() {
        return roles;
    }
    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }
    
    
    
    
    
    public static SecurityContextType randomOne () {
        SecurityContextType result = new SecurityContextType();
        result.setContextType("sc-contextType-"  + SecurityContextManager.random.nextInt());
        result.setDisplayName("sc-displayName-"  + SecurityContextManager.random.nextInt());
        List<Role> roles = new ArrayList<Role>();
        roles.add(Role.randomOne());
        roles.add(Role.randomOne());
        result.setRoles(roles);
        
        return result;
    }


    

	

	@Override
	public String toString() {
		return "SecurityContext [contextType=" + contextType + ", displayName="
				+ displayName + ", roles=" + roles + "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((contextType == null) ? 0 : contextType.hashCode());
		result = prime * result
				+ ((displayName == null) ? 0 : displayName.hashCode());
		result = prime * result + ((roles == null) ? 0 : roles.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		SecurityContextType other = (SecurityContextType) obj;
		if (contextType == null) {
			if (other.contextType != null)
				return false;
		} else if (!contextType.equals(other.contextType))
			return false;
		if (displayName == null) {
			if (other.displayName != null)
				return false;
		} else if (!displayName.equals(other.displayName))
			return false;
		if (roles == null) {
			if (other.roles != null)
				return false;
		} else if (!roles.equals(other.roles))
			return false;
		return true;
	}
	/**
	 * Load SecurityContext object from reader
	 * @param reader
	 * @return
	 * @throws JAXBException
	 */
	public static SecurityContextType load(Reader reader) throws JAXBException {
        JAXBContext jaxb = JSONJAXBContext.newInstance(
        		SecurityContextType.class
        		, Role.class
                , SecurityObject.class
        );
        JSONUnmarshaller unmarshaller = JSONJAXBContext.getJSONUnmarshaller(jaxb.createUnmarshaller());
        return unmarshaller.unmarshalFromJSON(reader, SecurityContextType.class);
		
	}

}
